/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accesoDatos.negocio.DAOS;

import accesoDatos.negocio.entity.Precio;
import java.io.Serializable;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author ian
 */
public class PrecioDao implements Serializable {


     public String create(Precio precio, EntityManager emc) {
        emc.getTransaction().begin();
        try {
            precio = emc.merge(precio);
            emc.persist(precio);
            emc.getTransaction().commit();
            return "Sucess";
        } catch (Exception e) {
            System.out.println(e);
            emc.getTransaction().rollback();
            return "Fail";
        }
    }


 public void update(Double volume, Precio nuevo, EntityManager em){
        em.getTransaction().begin();
        try{
            Query q = em.createQuery("UPDATE Precio s SET s.low = '" +
                    nuevo.getLow() + "', s.high = '"+ nuevo.getHigh() +
                    "', s.open = '"+ nuevo.getOpen() + "', s.close = '"+
                    nuevo.getClose()+"', s.volume = '"+ nuevo.getVolume()  +
                    "' WHERE s.volume= '" + volume + "'");
            q.executeUpdate();
            em.getTransaction().commit();
        }catch(Exception e){
            System.out.println(e);
            em.getTransaction().rollback();
        }
    }
 
    public void delete(Precio precio, EntityManager em){
        this.delete(precio, em);
    }

    public Precio buscarNombre(Double volume, EntityManager em){
        try{
            Query q = em.createQuery("SELECT u FROM Precio as u WHERE u.volume = '" + volume + "'");
            return (Precio)q.getSingleResult();
        }catch(Exception e){
            System.out.println(e);
            return null;
        }
    }
}
